﻿using System.IO;
using System.Threading.Tasks;
using DataMongo;
using DataMongo.Entities;
using ExtendedMongoMembership;

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MVCMongo.Controllers
{
    [Authorize]
    public class MongoController : Controller
    {
        //MongoDBEntities db = new MongoDBEntities();
        //
        // GET: /Mongo/

        public ActionResult Index()
        {
           
            var connection = new  RepositoryBaseMongo("Department");
            var data=connection.GetAll<Department>();
            //var collection = from f in db.Departments
              //               select f;
            return View(data);
        }

        //
        // GET: /Mongo/Details/5

        public ActionResult Details(int id)
        {
            //Department dep = (from f in db.Departments
            //                  where f.DepartmentId == id
            //                  select f).First();
            return View();
        }

        //
        // GET: /Mongo/Create

        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /Mongo/Create

        [HttpPost]
        public ActionResult Create(Department dept, string picture)
        {
            try
            {
                var connection = new RepositoryBaseMongo("Department");
                var file = Request.Files[0];
                dept.Picture = picture;
                if (file != null && file.ContentLength > 0)
                {
                    dept.Picture = Path.GetFileName(file.FileName);
                    var path = Path.Combine(Server.MapPath("~/Images/"), dept.Picture);
                    file.SaveAs(path);
                }

                connection.InsertOrUpdate(dept);
                //db.CreateDepartment(dept);
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Mongo/Edit/5

        //public ActionResult Edit(int id = 0)
        //{
        //    var connection = new RepositoryBaseMongo("Department");
        //    List<Department> list = (from f in connection.GetAll<>()
        //                             where f.DepartmentId == id
        //                             select f).ToList();
        //    Department dept = new Department();
        //    if (list.Count > 0) dept = list[0];
        //    return View(dept);
        //}

        //
        // POST: /Mongo/Edit/5

        //[HttpPost]
        //public ActionResult Edit(int id, Department dept)
        //{
        //    try
        //    {
        //        db.EditDepartment(dept);
        //        return RedirectToAction("Index");
        //    }
        //    catch
        //    {
        //        return View();
        //    }
        //}

        //
        // GET: /Mongo/Delete/5

        //public ActionResult Delete(int id)
        //{
        //    Department dep = (from f in db.Departments
        //                      where f.DepartmentId == id
        //                      select f).First();
        //    return View(dep);
        //}

        //
        // POST: /Mongo/Delete/5

        //[HttpPost]
        //public ActionResult Delete(int id, Department collection)
        //{
        //    try
        //    {
        //        Department dep = (from f in db.Departments
        //                          where f.DepartmentId == id
        //                          select f).First();
        //        db.DeleteDepartment(dep);
        //        return RedirectToAction("Index");
        //    }
        //    catch
        //    {
        //        return View();
        //    }
        //}

        //protected override void Dispose(bool disposing)
        //{
        //    if (db == null) db.Dispose();
        //    base.Dispose(disposing);
        //}
    }
}
